<?php
/**
 * Created by: shauan.
 * Date: 17/08/14 - 22:57
 */

namespace user\Controller;

use src\AppController;

class UserAppController extends AppController {
    /** codigo tipo usuario cooperado */
    const TIPO_USER_COO = 2;
    /** codigo tipo usuario beneficiario */
    const TIPO_USER_BEN = 21;
    /** codigo tipo usuario empresa */
    const TIPO_USER_EMP = 41;
    /** codigo tipo usuario coolaborador */
    const TIPO_USER_INT = 1;
    /** codigo tipo usuario prestador */
    const TIPO_USER_PRE = 42;

    protected function startSessionSystem(){
        $classLogin = "user\\Controller\\LoginController";
        $acao = $this->request->base->param_url['action'];
        $method = $acao."Action";
        if(method_exists($classLogin,$method)){
            $acao = strtolower($acao);
            switch($acao){
                case 'unimed':
                    $this->Session->write('SYSTEM',['BASE'=>'uniweb','LOCAL'=>'unimed','CODE'=>'0099']);
                    return true;
                    break;
                case 'intermed':
                    $this->Session->write('SYSTEM',['BASE'=>'intweb','LOCAL'=>'intermed','CODE'=>'0933']);
                    return true;
                    break;
            }
        }
        return false;
    }


    protected function UserAuthorization($array)
    {
        $module = strtolower($array['module']);
        $controller = $array['controller'];
        $action = $array['action'];
        $ObjController = $module.'\\Controller\\'.$controller."Controller";
        $session = $this->Session->session();

        $authenticated = $this->Session->read('authe.AUTHENTICATED');
        if ($authenticated === true) {
            if (!empty($session)) {
                if (!method_exists($ObjController, $action . "Ajax")) {
                    $session = $this->Session->session();
                    if (!empty($session)) {
                        $authenticated = $this->Session->read('authe.AUTHENTICATED');
                        if ($authenticated === true) {
                            $permission = $this->Session->read('authe.USER.PERMISSION');
                            $permission = $permission['MODULE'][$module]['CONTROLLER'][$controller]['ACTION'];
                            if (is_array($permission)) {
                                if (in_array($action, $permission)) {
                                    return true;
                                }
                            }
                        } else {
                            $this->Session->setFlash($this->msgType("login"), "info");
                            $this->redirect(array('action' => $this->Session->read('SYSTEM.LOCAL'), 'controller' => 'login', 'module' => 'user'));
                        }
                    } else {
                        $this->Session->setFlash($this->msgType("permissao"), "info");
                        $this->redirect(array('action' => $this->Session->read('SYSTEM.LOCAL'), 'controller' => 'login', 'module' => 'user'));
                    }
                    $this->Session->setFlash($this->msgType("permissao"), "info");
                    $this->redirect(array('action' => 'index', 'controller' => 'index', 'module' => $this->Session->read('authe.USER.GROUP.MODULE_PRIMARY_PATH')));
                }
            }else{

                $this->redirect(array('url' => $this->getUrl()));
            }
        }else{
            $this->redirect(array('url' => $this->getUrl()));
        }

    }
}